﻿using INTEX.DataBase.Tools;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Linq;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    public partial class ITXEXRPT
    { 
         
        private void TSTVK04(DataSet db)  
        {
            string msg = "";
            string sdkey = "";
            try
            {
                if (!dsSource.Tables.Contains("VAART"))
                {
                    return;
                }
                if (!dsSource.Tables.Contains("VAFARBEN"))
                {
                    return;
                }
                string src = "";
                string sgb = "";
                DataTable tbVKKOPF = dsSource.Tables["VKKOPF"];
                DataTable tbVKPOS = dsSource.Tables["VKPOS"];
                DataRow dwVKPOS1 = tbVKPOS.Rows[0];
                string KRS;
                string LFD;
                string POS;
                string TXTISIND;
                string str1 = "", str2 = "";
                int position = 0; 
                string currency="RMB";
                DataRow dwVKKOPF = tbVKKOPF.Rows[0];
                DataTable tbVKPBEST = new DataTable(), tbVKK07 = new DataTable();
                DataTable TTGAAUF = new DataTable();
                string VKPMEEH = "";
                DataRow dwVKP = tbVKPOS.Rows[0];
                string printdate;
                string text = "";
                int positions = 0;
                DataTable tbVA07 = dsSource.Tables["VAART"];
                DataTable tbVA10 = dsSource.Tables["VAFARBEN"];
                DataRow dwVA07 = tbVA07.Rows[0];
                DataRow dwVA10 = null;
                double VKKQTY = 0;
                string meeh = "MTR";
                string yporder = "";
                #region=============VKKOPF==================

                //src = "确认号码/Proforma Invoice：" + dwVKKOPF["VKKAUFKRS"].ToString() + "-" + System.Convert.ToInt64(dwVKKOPF["VKKAUFLFD"].ToString());
                src = dwVKKOPF["VKKAUFKRS"].ToString() + "-" + Right(dwVKKOPF["VKKAUFLFD"].ToString(), 4);
                exAgent.SetCellValue(4, 36, src,true);
                exAgent.SetCellValue(5, 36, date);
                src = dwVKKOPF["VKKBESTNR"].ToString();
                exAgent.SetCellValue(7, 36, src); 
                src = dwVKKOPF["VKKBESTDAT"].ToString();
                exAgent.SetCellValue(8, 36, src);

                DataRow dwKD03=null;
                var qkd = from dwTemp in dsSource.Tables["KDSTAMM"].AsEnumerable()
                          where dwTemp["KDKUNDNR"].ToString()==dwVKKOPF["VKKKUNDNR"].ToString()
                            select dwTemp;
                foreach (DataRow row in qkd)
                {
                    dwKD03 = row;
                }
                if (dwKD03 != null)
                {
                 //   src = "客户编号/Client No:";
                    //exAgent.SetCellValue(8, 30, src); 
                    exAgent.SetCellValue(6, 36, dwKD03["KDKUNDNR"].ToString());
                    //src = dwKD03["KDKNAME"].ToString();
                    //exAgent.SetCellValue(4, 7, Helper.GetGB2312Value(src).Trim());
                    src = dwKD03["KDNAME1"].ToString();
                    exAgent.SetCellValue(4, 7, Helper.GetGB2312Value(src).Trim());
                    src = dwKD03["KDSTRASSE1"].ToString();
                    exAgent.SetCellValue(5, 7, Helper.GetGB2312Value(src).Trim());
                  src = dwKD03["KDTEL"].ToString();
                    exAgent.SetCellValue(6, 7, Helper.GetGB2312Value(src).Trim());
                    src = dwKD03["KDFAX"].ToString();
                    exAgent.SetCellValue(6, 18, Helper.GetGB2312Value(src).Trim());
                     /* src = dwKD03["KDMAILADR"].ToString();
                    exAgent.SetCellValue(10, 1, Helper.GetGB2312Value(src).Trim()); */
                }
                
                src =Helper.GetHRName(dsSource, dwVKKOPF["VKKSACHBE"].ToString()); 
                exAgent.SetCellValue(9, 36, src);
                currency = dwVKKOPF["VKKHWAEHRSL"].ToString();
                VKPMEEH = dwVKP["VKPMEEH"].ToString();
                meeh = dwVKPOS1["VKPPREH"].ToString();
                exAgent.SetCellValue(30, 17, "QTTY" +Environment.NewLine+ VKPMEEH);
                exAgent.SetCellValue(30, 23, "PRICE" + Environment.NewLine + currency + "/" + meeh);
                exAgent.SetCellValue(30, 28, "TOTAL" + Environment.NewLine + currency);
                 #endregion
                 
                //exAgent.SetCellValue(x+4, 1, "Terms of payment:");
                src = dwVKKOPF["VKKZAHLBED"].ToString();
                var qz = from dwTemp in dsSource.Tables["ICC_TXTKON"].AsEnumerable()
                         where dwTemp["TXTIPIND"].ToString().Trim() == src.Trim() + "E"
                         select dwTemp;
                foreach (DataRow row in qz)
                {
                    exAgent.SetCellValue(114, 11, Helper.GetGB2312Value(row["TXT010"].ToString()));
                }

                src = dwVKKOPF["VKKVERSART"].ToString();
                sdkey = "00" + Left(src + "                                           ", 21) + "E";
                src = Helper.GetSDDAT(dsSource, "0208", src, "E").PadRight(20); ;
                exAgent.SetCellValue(115, 11, Helper.GetGB2312Value(src.Substring(1, 10)));

                //exAgent.SetCellValue(106, 1, "发货条件/Delivery Terms:");
                src = dwVKKOPF["VKKLIEFBED"].ToString();
                src = Helper.GetSDDAT(dsSource, "0241", src, "E").PadRight(30); ;
                exAgent.SetCellValue(116, 11, Helper.GetGB2312Value(src.Substring(1, 20)));
                //exAgent.SetCellValue(940, 46, prdg);

                #region============组织规格VAD===================
                tbVKK07 = new DataTable();
                string koll = dwVKP["VKPKOLL"].ToString();
                string art = dwVKP["VKPART"].ToString();
                text = "";
                TXTISIND = koll + art.Trim();
                var qvad = from dwTemp in dsSource.Tables["ICC_TXTKON"].AsEnumerable()
                           where dwTemp["TXTIPIND"].ToString().Trim() == TXTISIND
                           select dwTemp;
                if (qvad.Count() > 0)
                {
                    tbVKK07 = qvad.CopyToDataTable();
                }
                foreach (DataRow dwTXTKON in tbVKK07.Rows)
                {
                    for (int ti = 1; ti < 40; ti++)
                    {
                        string ci = "TXT" + Right("00" + ti, 2) + "0";
                        src = dwTXTKON[ci].ToString(); src = GetGB2312Value(src).Trim();
                        if (src == "")
                            break;
                        text += src;
                    }
                }
                exAgent.SetCellValue(11, 8, koll);
                exAgent.SetCellValue(12, 8, text);                
                #endregion
                #region================成份===================
                src = Helpers.Helper.GetCompositionVA(dsSource, dwVA07);
                exAgent.SetCellValue(14, 8, src);
                #endregion

                #region============其他要求ANGE===================
                tbVKK07 = new DataTable();
                KRS = dwVKKOPF["VKKAUFKRS"].ToString();
                LFD = dwVKKOPF["VKKAUFLFD"].ToString();
                TXTISIND = KRS + LFD + "000" + "ANGE";
                var qvor = from dwTemp in dsSource.Tables["ICC_TXTKON"].AsEnumerable()
                           where dwTemp["TXTIPIND"].ToString().Contains(TXTISIND)
                           select dwTemp;
                if (qvor.Count() > 0)
                {
                    tbVKK07 = qvor.CopyToDataTable();
                }

                text = "";
                foreach (DataRow dwTXTKON in tbVKK07.Rows)
                {
                    for (int ti = 1; ti < 40; ti++)
                    {
                        string ci = "TXT" + Right("00" + ti, 2) + "0";
                        src = dwTXTKON[ci].ToString(); sgb = GetGB2312Value(src).Trim();
                        if (sgb.Trim() == "")
                            continue;
                        exAgent.SetCellValue(20 - 1 + ti, 5, sgb);
                    }
                }
                #endregion
      
                #region============花型要求IVVT===================

                DataTable tbSTD = new DataTable();
                KRS = dwVKKOPF["VKKAUFKRS"].ToString();
                LFD = dwVKKOPF["VKKAUFLFD"].ToString();
                TXTISIND = KRS + LFD + "000" + "IVVT";
                var query7 = from dwTemp in dsSource.Tables["ICC_TXTKON"].AsEnumerable()
                             where dwTemp["TXTIPIND"].ToString().Contains(TXTISIND)
                             select dwTemp;
                if (query7.Count() > 0)
                {
                    tbSTD = query7.CopyToDataTable();
                }
                #endregion 

               int xStart = 31;
                int x = xStart;
                string finish = "";
                double TAmount=0;
                int count = tbVKPOS.Rows.Count;
                DataRow dwKDART = null;
                foreach (DataRow dwVKPOS in tbVKPOS.Rows)
                {
                    dwKDART = null;
                    string color="";
                    positions++;
                    msg = "开始写入：" + position.ToString() + "行";
                    EventDispatcher.Instance.RaiseStatus(msg, StatusEventType.StopProgress);
                    KRS = dwVKPOS["VKPAUFKRS"].ToString();
                    LFD = dwVKPOS["VKPAUFLFD"].ToString();
                    POS = dwVKPOS["VKPAUFPOS"].ToString();
                    POS = dwVKPOS["VKPAUFPOS"].ToString();
                    exAgent.SetCellValue(x, 1, System.Convert.ToInt64(POS));
                 
                    #region====================VKPOS=======================
                    src = dwVKPOS["VKPART"].ToString().TrimEnd();
                    sgb = GetGB2312Value(src);
                    exAgent.SetCellValue(x, 3, sgb); 
                    #endregion
                    #region================组织===================
                    dwVA07 = null;
                    var q07 = from DataRow row in tbVA07.AsEnumerable()
                              where row["VAAART"].ToString() == dwVKPOS["VKPART"].ToString()
                              select row;
                    foreach (DataRow row in q07)
                    {
                        dwVA07 = row;
                    }
                    dwVA10 = null;
                    var q10 = from DataRow row in tbVA10.AsEnumerable()
                              where row["VAFART"].ToString() == dwVKPOS["VKPART"].ToString()
                              && row["VAFFARBE"].ToString() == dwVKPOS["VKPFARBE"].ToString()
                              select row;
                    foreach (DataRow row in q10)
                    {
                        dwVA10 = row;
                    }

                    src = dwVA10["VAFBESCHICH"].ToString().Trim();
                    sdkey = "00" + Left(src + "                                           ", 21);
                    src = INTEXDBC.GetSDDAT("0275", sdkey);
                    src = Helper.GetGB2312Value(src.Substring(69, 10)).Trim();
                    int idx = 0;
                    idx = src.IndexOfAny(new char[] { '(', '（' });
                    if (idx > 0)
                        src = src.Substring(0, idx);
                    if (!finish.Contains(src + ";"))
                        finish += src + ";";
                    color += src.Trim();
                    //exAgent.SetCellValue(x, 8, src.Trim());
                    exAgent.SetCellValue(11, 18, finish);
                    #endregion

                    #region================客人花号/品名======================
                    src = dwVKPOS["VKPKDARTNR"].ToString().TrimEnd() + " " + dwVKPOS["VKPKDBEZ"].ToString().TrimEnd() + " " + dwVKPOS["VKPKDFARBNR"].ToString().TrimEnd();
                    var qvakd = from DataRow row in dsSource.Tables["VAKDBEZ"].AsEnumerable()
                              where row["VAKBKOLL"].ToString() == dwVKPOS["VKPKOLL"].ToString()
                              && row["VAKBART"].ToString() == dwVKPOS["VKPART"].ToString()
                              && row["VAKBFARBE"].ToString() == dwVKPOS["VKPFARBE"].ToString()
                              select row;
                    foreach (DataRow rkd in qvakd)
                    {
                        src = rkd["VAKBKDARTNR"].ToString().TrimEnd() + " " + rkd["VAKBKDARTBEZ"].ToString().TrimEnd() + " " + rkd["VAKBKDFARBNR"].ToString().TrimEnd();
                        dwKDART = rkd;
                    }
                    sgb = GetGB2312Value(src);
                    exAgent.SetCellValue(x + 1, 3, sgb);
                    #endregion
                    #region================数量===================
                    src = dwVKPOS["VKPMENGE1"].ToString();
                    string VKPMENGE1 = Divide(src, 100);
                    VKKQTY += System.Convert.ToDouble(VKPMENGE1);
                    
                    exAgent.SetCellValue(x, 17, VKPMENGE1);  
                    
                    src = dwVKPOS["VKPPREIS"].ToString();                    
                    exAgent.SetCellValue(x, 23, Divide(src, 1000));                    
                    src = dwVKPOS["VKPPOSWERT"].ToString();  
                    TAmount+= System.Convert.ToDouble(src);
                    exAgent.SetCellValue(x, 28, src);
                    #endregion 
                    #region =================交期======================
                    exAgent.SetCellValue(x, 36, dwVKPOS["VKPWUTERM"].ToString());
                    exAgent.SetCellValue(x + 1, 36, "Confirm:"+dwVKPOS["VKPLIEFTERM"].ToString(),true);
                    //VKPWUTERM//
                    //船样 
                    #endregion
                    #region =================标准====================
                    text = "";
                    string std = GetGB2312Value(dwVKPOS["VKPZUSATZ"].ToString().Trim());
                    foreach (DataRow dwTXTKON in tbSTD.Rows)
                    {
                        for (int ti = 1; ti < 40; ti++)
                        {
                            string ci = "TXT" + Right("00" + ti, 2) + "0";
                            src = dwTXTKON[ci].ToString(); sgb = GetGB2312Value(src).Trim();
                            text = sgb.TrimEnd();
                            if (text.Trim() == "")
                                break;
                            int idxd = text.IndexOf(".");
                            if (idxd > 0)
                            {
                                string line = text.Substring(0, idxd);
                                line = Right("000" + line, 3);
                                if (line == POS)
                                    std += text.Substring(idxd + 1);
                            }
                        }
                    }
                    exAgent.SetCellValue(x, 25, std);
                    #endregion
                    if (positions < count)
                    {
                        exAgent.RangeCopy(x, x + 1, x + 2, x + 3);
                    } 
                    x = x + 2;
                }
                if (meeh.Trim() == "YRD")
                {
                    //VKKQTY = System.Math.Round(System.Convert.ToDouble(VKKQTY) * 0.9144, 0);
                }
     
                exAgent.SetCellValue(x+2, 14, "Total quantity:"+VKKQTY + VKPMEEH,true);
                exAgent.SetCellValue(x+2, 28, "Total value:"+TAmount + currency,true);
     

                FillFooter(x + 8);
                /*src = dwVKKOPF["VKKSACHBE"].ToString();
                src = Helper.GetHRName(dsSource, src);
                exAgent.SetCellValue(x + 8, 6, src);*/
            }
            catch (Exception ex)
            {
                msg = "有错误产生，请注意:";
                EventDispatcher.Instance.RaiseStatus(msg, StatusEventType.StopProgress);
                Tracer.Error(ex.StackTrace, ex);
                
            }
            finally
            {
                msg = "指令表虽然已经生产，但请注意核对。";
                EventDispatcher.Instance.RaiseStatus(msg, StatusEventType.StopProgress);
            }
        }
        public void FillFooter(int x)
        {
            /*
            exAgent.SetCellValue(x, 1, "营销人员:");
            exAgent.SetCellValue(x, 11, "工厂负责人:");
            exAgent.SetCellValue(x, 21, "制单:");
            exAgent.SetCellValue(x, 24, Helper.GetHRName(dsSource, UserID));
            exAgent.SetCellValue(x, 31, "审核:");*/
        }
       
    }
}
